package com.zlfund.cat.zjgsmk;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.zlfund.cat.zjgsmk.api.ZjgsmkService;
import com.zlfund.cat.zjgsmk.api.ZjgsmkService.ZGetStartAdService;
import com.zlfund.cat.zjgsmk.bean.StartUpAdBean;
import com.zlfund.cat.zjgsmk.constant.Constants;
import com.zlfund.cat.zjgsmk.net.INetCallback;
import com.zlfund.cat.zjgsmk.net.RequestCall;
import com.zlfund.cat.zjgsmk.net.RetrofitNoParamRequest;
import com.zlfund.cat.zjgsmk.net.RxNetErrorEmptyImp;
import com.zlfund.cat.zjgsmk.ui.base.BaseActivity;
import com.zlfund.cat.zjgsmk.util.GlideUtil;
import com.zlfund.cat.zjgsmk.util.Logger;
import com.zlfund.cat.zjgsmk.util.SpUtils;
import com.zlfund.cat.zjgsmk.util.timetask.TimerService;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class SplashActivity extends BaseActivity {

    @BindView(R.id.iv_skip)
    TextView mIvSkip;
    @BindView(R.id.iv_ad)
    ImageView mIvAd;

    private static final long AD_SHOW_TIME = 3000;
    private String mPreAdUrl;
    private RequestCall<StartUpAdBean, ZGetStartAdService> mCall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_splash);
    }

    @Override
    protected void setListener() {
        final TimerService service = new TimerService();

        Observable<Long> observable = Observable.create(new ObservableOnSubscribe<Long>() {
            @Override
            public void subscribe(@NonNull final ObservableEmitter<Long> e) throws Exception {
                e.onNext(0L);
                service.timeSchedule(new Runnable() {
                    @Override
                    public void run() {
                        e.onNext(service.getTotalTime());
                    }
                }, 1000);
            }
        });
        observable.observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        mIvSkip.setText(String.format(mContext.getResources().getString(R.string.time_unit_second), (AD_SHOW_TIME - aLong) / 1000));
                        if (aLong >= AD_SHOW_TIME) {
                            service.cancel();
                            mContext.startActivity(new Intent(mContext, LeadActivity.class));
                            finish();
                        }
                    }
                }, new RxNetErrorEmptyImp());
    }

    @Override
    protected void initData() {
        mPreAdUrl = SpUtils.getStringFromGlobal(mContext, Constants.PRE_AD_LINK);
        if (mPreAdUrl != null) {
            GlideUtil.loadUrl(mContext, mPreAdUrl, mIvAd);
        } else {
            GlideUtil.loadRes(mContext, R.mipmap.welcome, mIvAd);
        }

        mCall = new RetrofitNoParamRequest<StartUpAdBean>()
                .build(ZGetStartAdService.class);
        mCall.enqueueWithRx("getAdInfo", new INetCallback<StartUpAdBean>() {
            @Override
            public void response(StartUpAdBean startUpAdBean) {
                String ico = startUpAdBean.getAdvertisement().get(0).getIco();
                Logger.i(ico);
                if (mPreAdUrl == null || (mPreAdUrl != null && !mPreAdUrl.equals(ico))) {
                    GlideUtil.loadUrl(mContext, ico, R.mipmap.welcome, mIvAd);
                    SpUtils.putStringToGlobal(mContext, Constants.PRE_AD_LINK, ico);
                }
            }

            @Override
            public void error(String str) {

            }
        });

    }

    @OnClick(R.id.iv_skip)
    public void onViewClicked() {
        mCall.cancel();
    }
}
